package com.flower.enjoyment.flower.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flower.enjoyment.flower.entity.OrderInfo;
import com.flower.enjoyment.flower.entity.UserInfo;
import com.flower.enjoyment.flower.entityDo.OrderInfoDo;
import com.flower.enjoyment.flower.entityDo.ShopInfoDo;
import com.flower.enjoyment.flower.entityDo.UserInfoDo;
import com.flower.enjoyment.flower.mapper.OrderInfoMapper;
import com.flower.enjoyment.flower.mapper.ShopMapper;
import com.flower.enjoyment.flower.mapper.UserInfoMapper;
import com.flower.enjoyment.flower.service.OrderInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OrderInfoServiceImpl implements OrderInfoService {
    protected static final Logger logger = LoggerFactory.getLogger(OrderInfoServiceImpl.class);
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    /**
     * 获取用户常去商铺
     *
     * @param pageNo
     * @param pageSize
     * @param userId
     * @return
     */
    @Override
    public Page<OrderInfoDo> getSuccessOrderListByPage(Integer pageNo, Integer pageSize, String userId) {
        PageHelper.startPage(pageNo, pageSize);
        return orderInfoMapper.getSuccessOrderListByPage(userId);
    }

    /**
     * 商铺收入列表
     *
     * @param pageNo
     * @param pageSize
     * @param shopId
     * @param type
     * @return
     */
    @Override
    public Page<OrderInfoDo> getShopIncomeOrderListByPage(Integer pageNo, Integer pageSize, String shopId, String type) {
        PageHelper.startPage(pageNo,pageSize);
        return orderInfoMapper.getShopIncomeOrderListByPage(shopId, type);
    }

    /**
     * 保存订单
     *
     * @param orderInfo
     * @return
     */
    @Override
    @Transactional
    public int insertOrderInfo(OrderInfo orderInfo) {

        return orderInfoMapper.insert(orderInfo);
    }

    /**
     * 根据订单号获取订单信息
     *
     * @param orderNo
     * @return
     */
    @Override
    public OrderInfoDo selectByOrderNo(String orderNo) {
        return orderInfoMapper.selectByOrderNo(orderNo);
    }

    /**
     * 更新订单信息
     *
     * @param orderInfo
     * @return
     */
    @Override
    @Transactional
    public int updateOrderInfo(OrderInfo orderInfo) {
        logger.info("更新订单传入的订单信息：{}",JSONObject.toJSONString(orderInfo));
        ShopInfoDo shopInfoDo = shopMapper.selectByShopId(orderInfo.getShopId());
        logger.info("更新订单根据订单的商铺ID获取商铺信息：{}", JSONObject.toJSONString(shopInfoDo));
        UserInfoDo bossInfoDo = userInfoMapper.selectByPrimaryKey(shopInfoDo.getUserId());
        logger.info("更新订单根据商铺绑定的用户ID获取用户信息：{}", JSONObject.toJSONString(bossInfoDo));
        UserInfo bossInfo = new UserInfo();
        bossInfo.setId(shopInfoDo.getUserId());
        bossInfo.setIncomeBalance(bossInfoDo.getIncomeBalance()+orderInfo.getActualAmount());
        bossInfo.setTodayIncome(bossInfoDo.getTodayIncome()+orderInfo.getActualAmount());
        bossInfo.setTotalIncome(bossInfoDo.getTotalIncome()+orderInfo.getActualAmount());
        userInfoMapper.updateByPrimaryKeySelective(bossInfo);
        return orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
    }

    /**
     * 根据用户id和商铺id判断该用户是否再此店铺消费过
     *
     * @param userId
     * @param shopId
     * @return
     */
    @Override
    public int selectCountByUserIdShopId(String userId, String shopId) {
        return orderInfoMapper.selectCountByUserIdShopId(userId, shopId);
    }

    /**
     * 今日收入
     *
     * @param userId
     * @return
     */
    @Override
    public double selectSumAmountByToday(String userId) {
        return orderInfoMapper.selectSumAmountByToday(userId);
    }

    /**
     * 全部收入
     *
     * @param userId
     * @return
     */
    @Override
    public double selectSumAmountByAll(String userId) {
        return orderInfoMapper.selectSumAmountByAll(userId);
    }

    /**
     * 将订单置为过期
     *
     * @return
     */
    @Override
    public int updateExpireOrder() {
        return orderInfoMapper.updateExpireOrder();
    }
}
